package com.n9.qqonline.sales.service;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 求购信息统计
 * @author ice
 * @since 2019/3/12 9:43
 */
public class BuyMsgReport {
    private static final BuyMsgReport me = new BuyMsgReport();

    private AtomicLong unReadCount = new AtomicLong(0);
    private AtomicLong totalCount = new AtomicLong(0);

    private BuyMsgReport.ReportView reportView;

    private BuyMsgReport() {
    }

    /**
     * 获取实例
     * @return
     */
    public static final BuyMsgReport instance() {
        return me;
    }

    /**
     * 设置报表view
     * @param reportView
     */
    public void setReportView(ReportView reportView) {
        this.reportView = reportView;
    }


    /**
     * 设置未读消息数量
     * @param totalCount
     * @param unReadCount
     */
    public void setUnreadCount(long totalCount, long unReadCount) {
        this.unReadCount.set(unReadCount);
        this.totalCount.set(totalCount);
        viewReport();
    }

    /**
     * 获取未读消息数量
     * @return
     */
    public long getUnReadCount() {
        return unReadCount.get();
    }

    /**
     * 获取消息总数
     * @return
     */
    public long getTotalCount() {
        return this.totalCount.get();
    }

    /**
     * 自增未读数量
     * @return
     */
    public long incUnReadCount() {
        totalCount.incrementAndGet();
        long count = unReadCount.incrementAndGet();
        viewReport();
        return count;
    }

    private void viewReport() {
        if (Objects.nonNull(reportView)) {
            reportView.showNewReport(this);
        }
    }

    /**
     * 报表显示接口
     */
    interface ReportView {
       void showNewReport(BuyMsgReport buyMsgReport);
    }

}
